#include "HelloWorldScene.h"

USING_NS_CC;

#define PLANE_TAG	1

/*********************| static func |*********************/

CCNode* plane_emitBullet(const int buID, const char* fname, const char* objdatana)
{
//	CCSprite* r =  CCSprite::createWithSpriteFrameName("npc_zd_5.png");
//	r->setAnchorPoint(ccp(0, 0.5));
//	return r;
	return CCSprite::create("bullet.png");
}

void plane_orcp(const int buID, const char *fname, const char *objdatana, convex_polygon_t *out)
{
	CCTexture2D* t = CCTextureCache::sharedTextureCache()->textureForKey("bullet.png");
	CSB::CCSize2CP(t->getContentSize(), out);
	out->pnum = 5;
	out->points[0].x = out->points[4].x = -12.0f;
	out->points[0].y = out->points[1].y = 7.5f;
	out->points[1].x = out->points[3].x = 2.0f;
	out->points[2].x = 12.0f;
	out->points[2].y = 0.0;
	out->points[3].y = out->points[4].y = -7.5f;

}
/*********************| functions in HelloWorld |*********************/

CCScene* HelloWorld::scene()
{
	CCScene *scene = CCScene::create();
	HelloWorld *layer = HelloWorld::create();
	scene->addChild(layer);
	return scene;
}

bool HelloWorld::init()
{
	if ( !CCLayer::init() ) {
		return false;
	}
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
	CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
	                                      "CloseNormal.png",
	                                      "CloseSelected.png",
	                                      this,
	                                      menu_selector(HelloWorld::menuCloseCallback));

	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
	                            origin.y + pCloseItem->getContentSize().height/2));
	CCMenuItemFont* pUb = CCMenuItemFont::create("unbind", this, menu_selector(HelloWorld::unBindBtnCB));
	pUb->setAnchorPoint(ccp(0, 0));
	pUb->setPosition(100, 20);
	pUb->setColor(ccRED);

	CCMenu* pMenu = CCMenu::create(pCloseItem, pUb, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, 1);

	CCSprite* pSprite = CCSprite::create("HelloWorld.png");
	pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
	this->addChild(pSprite, 0);
	///////////////////////////////////////////////////////////////////////////
	//1. load a csb
	CSB_NO_RET(CSB::cc2dv2::instance()->loadfile("bullets/boss_4.csb"), false);
	//2. create a sprite and bullet node show plane and bullets
//	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("NPC_ZD.plist");
//	CCSpriteBatchNode* buNode = CCSpriteBatchNode::create("NPC_ZD.png", 200);
	CCSpriteBatchNode* buNode = CCSpriteBatchNode::create("bullet.png", 200);
//	CCNode* buNode = CCNode::create ();
	buNode->setPosition(CCPointZero);
	this->addChild(buNode);

	CCSprite* plane = CCSprite::create("plane.png");
	plane->setPosition(pSprite->getPosition());
	this->addChild(plane, 0, PLANE_TAG);

	CCSprite* skype = CCSprite::create("skype.png");
	this->addChild(skype, 0, 19);
	skype->setPosition(ccp(visibleSize.width / 2, visibleSize.height - 200));
//	skype->runAction(CCRepeatForever::create(CCRotateBy::create(5.0f, 360.0f)));

	//3. do bind
	_myBind = CSB::BindProto::newBind();
	_myBind->fname = "boss_4";

	// 多组绑定示例
	ZERO_MALLOC(bele, CSB::BindProto::bindele_t);
	CSB::cc2dv2::CSMakeObjDataName(EMITTER, 4, bele->objdatana, sizeof(bele->objdatana));
	bele->flag = CSB::BindProto::OBJ_COLOR_BLEND | CSB::BindProto::BULLET_COLOR_BLEND | CSB::BindProto::UPDATE_PASSIVE ;
//	bele->angleOffset = 90;
//	bele->posOffset.x = -450;
	_myBind->eleList.push_back(bele);

	// 多组绑定示例
//	ZERO_MALLOC(cele, CSB::BindProto::bindele_t);
//	CSB::cc2dv2::CSMakeObjDataName(EMITTER, 1, cele->objdatana, sizeof(cele->objdatana));
//	cele->flag = CSB::BindProto::OBJ_COLOR_BLEND | CSB::BindProto::BULLET_COLOR_BLEND | CSB::BindProto::UPDATE_PASSIVE ;
//	cele->angleOffset = 90;
//	cele->posOffset.x = -450;
//	_myBind->eleList.push_back(cele);

	CSB::BulletProto planeBuPro = {plane_emitBullet, plane_orcp};
	CSB::DefaultBindProto* dp = new CSB::DefaultBindProto(skype, buNode, planeBuPro, this, HIT_CHECK_SELECTOR(HelloWorld::hitCheck));
	dp->setAutoMachine(this, AUTO_MACHINE_SELECTOR(HelloWorld::autoMachine));
	_myBind->proto = dp;
	if (!CSB::cc2dv2::instance()->bind(_myBind)) {
		CCLog("bind failed");
		delete _myBind;
		return false;
	}
	_hit = 0;

	CCLabelTTF* hitLbl = CCLabelTTF::create("0", "arial.ttf", 24);
	hitLbl->setAnchorPoint(ccp(0.5, 0));
	hitLbl->setPosition(ccp(pSprite->getPositionX(), 0));
	hitLbl->setColor(ccRED);
	this->addChild(hitLbl, 0,20);

	this->setTouchEnabled(true);
	this->scheduleOnce(schedule_selector(HelloWorld::beginFire), 1.0f);
	CCDrawNode* dnode = CCDrawNode::create();
	this->addChild(dnode, 20, 13);
	return true;
}


void HelloWorld::menuCloseCallback(CCObject* pSender)
{
	// release cc2dv2 plugin
	CSB::cc2dv2::end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
	CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
#endif
}

void HelloWorld::ccTouchesBegan(CCSet* pTouches, CCEvent* pEvent)
{
	CCSetIterator it = pTouches->begin();
	CCTouch* t = (CCTouch*)(*it);
	_lastPos = t->getLocation();
}

void HelloWorld::ccTouchesMoved(CCSet* pTouches, CCEvent* pEvent)
{
	CCSetIterator it = pTouches->begin();
	CCTouch* t = (CCTouch*)(*it);
	CCSprite* plane = (CCSprite*)this->getChildByTag(PLANE_TAG);
	plane->setPosition(ccpAdd(plane->getPosition(), ccpSub(t->getLocation(), _lastPos)));
	_lastPos = t->getLocation();
}

void HelloWorld::beginFire(float d)
{
	CSB::cc2dv2::instance()->run(_myBind, true);
#ifdef CSB_DEBUG
//	_myBind->proto->showBulletDebugCP(true);
#endif
}

void HelloWorld::unBindBtnCB(CCObject* p)
{
	if (_myBind) {
		CSB::cc2dv2::instance()->unbind(_myBind, false);
//		CSB::cc2dv2::instance()->unbindAll(true, true);
		/* it will free auto */
		_myBind = NULL;
	}
}

int HelloWorld::hitCheck(const convex_polygon_t* p)
{
	CCSprite* s = (CCSprite*)this->getChildByTag(PLANE_TAG);
	CCRECT_2_CP(s->boundingBox(), spcv);
//	CCPoint ps[16];
//	for (size_t i = 0; i < spcv.pnum; ++i) {
//		ps[i].x = spcv.points[i].x;
//		ps[i].y = spcv.points[i].y;
//	}
//	CCDrawNode* dnode = (CCDrawNode*)this->getChildByTag(13);
//	dnode->clear();
//	dnode->drawPolygon(ps, spcv.pnum,ccc4f(0, 1.0f, 0, 0.2f),
//				      1, ccc4f(1.0f,0,0, 1.0f));
	if (CP_love_CP(p, &spcv)) {
		++_hit;
		CCLabelTTF* pl = (CCLabelTTF*)this->getChildByTag(20);
		char buf[10] = {0};
		sprintf(buf, "%u", _hit);
		pl->setString(buf);
		s->setColor(ccc3(rand() % 155 + 100, rand() % 155 + 100, rand() % 155 + 100));

		return BULLET_UPDATE_DEAD;
	}
	return BULLET_UPDATE_CONTINUE;
}

void HelloWorld::autoMachine(pos_t* out)
{
	CCSprite* plane = (CCSprite*)this->getChildByTag(PLANE_TAG);
	out->x = plane->getPositionX();
	out->y = plane->getPositionY();
}



HelloWorld::~HelloWorld()
{
}
